package com.qxgltj.qxgl.service;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.qxgltj.qxgl.dao.IModulesDao;
import com.qxgltj.qxgl.dao.IRolesDao;
import com.qxgltj.qxgl.dto.ModulesDTO;
import com.qxgltj.qxgl.model.Modules;
import com.qxgltj.qxgl.model.Roles;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class ModulesServiceImpl implements IModulesService {
    @Resource
    private IModulesDao modulesDao;
    @Resource
    private IRolesDao rolesDao;
    @Override
    public Page<Modules> finPage(ModulesDTO dto) {
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "Id");
        PageRequest request = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), Sort.by(order));
        return modulesDao.findAll(getSpec(dto), request);
        
    }

    private Specification<Modules> getSpec(final ModulesDTO dto) {
        return new Specification<Modules>(){

            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Modules> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
              Predicate p=criteriaBuilder.conjunction();
              List<Expression<Boolean>> exList=p.getExpressions();
              if (dto.getName()!=null&&!"".equals(dto.getName())) {
                exList.add(criteriaBuilder.like(root.get("name"),"%"+dto.getName()+"%"));
                System.out.println("条件判断不为空");
            }
            if(dto.getParentId()!=null){
                exList.add(criteriaBuilder.equal(root.<Integer>get("parentId"), dto.getParentId()));
            }
            if (dto.getPath() != null && !"".equals(dto.getPath())) {
                exList.add(criteriaBuilder.equal(root.<String>get("path"), dto.getPath()));
            }
            if (dto.getWeight() != null && !"".equals(dto.getWeight())) {
                exList.add(criteriaBuilder.equal(root.<String>get("weight"), dto.getWeight()));
            }
                return p;
            }

        };
    }

    @Override
    public Modules saveModules(Modules m) {
        return modulesDao.save(m);
    }

    @Override
    public void del(String id) {
        modulesDao.deleteById(id);
    }

    @Override
    public void SetModules(String role_id, List<Integer> ModulesIdList) {
        Roles roles=rolesDao.getOne(role_id);
        for(Integer module_id:ModulesIdList){
        Modules modules=new Modules();
        modules.setId(module_id);
        roles.getModuleslist().add(modules);
    }
}
}